import Vue from 'vue'
import DialogVue from './dialog.vue'

const Diglog = function() {}
Diglog.show = (component, option = {}, data) => {
    let DialogVueConstructor = Vue.extend(DialogVue)
    let instance = new DialogVueConstructor().$mount()
    document.body.appendChild(instance.$el)
    let ComponentConstructor = Vue.extend(component)
    let componentInstance = new ComponentConstructor().$mount()
    Object.assign(instance.option, option)
    instance.option.visible = true
    if (data) componentInstance.data = data
    if (option) componentInstance.option = option
    instance.$nextTick(() => {
        if (instance.$refs.dialogBody) {
            instance.$refs.dialogBody.appendChild(componentInstance.$el)
        }
    })
    instance.confirmd = () => {
        instance.onDidConfirmd && instance.onDidConfirmd(data)
        instance.close()
    }
    instance.closed = () => {
        document.body.removeChild(instance.$el)
        let data = instance.data
        instance.onDidClose && instance.onDidClose(data)
    }
    componentInstance.$currentDialog = instance
    return instance
}

export default Diglog
